package com.rock.platform.action;

import java.io.File;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.rock.platform.entity.GridParamsEntity;
import com.rock.platform.entity.FilterEntity;
import com.rock.platform.entity.LoginBean;
import com.rock.platform.entity.SortEntity;

public class BaseAction extends ActionSupport {
	private static final long serialVersionUID = -3609342038414736L;

	protected LoginBean loginBean;
	protected String jsonString;
	protected String parentId;
	
	protected File uploadFile;
	protected String uploadFileFileName;
	protected String uploadFileContentType;
	
	protected InputStream excelStream;
	protected String exportFileName;
	
	private int start;
	private int limit;
	private int page;
	private String sort;
	private String filter;
	private GridParamsEntity gridParams = new GridParamsEntity();
	
	public LoginBean getLoginBean() {
		return loginBean;
	}
	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}
	public String getJsonString() {
		return jsonString;
	}
	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
		gridParams.setStart(this.start);
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
		gridParams.setLimit(this.limit);
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
		gridParams.setPage(this.page);
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
		gridParams.setSort(this.sort);
		
		if (this.sort != null && this.sort.length() > 0)
		{
			List<SortEntity> sortList = gridParams.getSortList();
			sortList.clear();
			JSONArray ja = JSONArray.fromObject(this.sort);
			for(int i = 0; i < ja.size(); i++)
			{
				JSONObject jo = ja.getJSONObject(i);
				SortEntity entity = new SortEntity();
				entity.setDirection(jo.getString("direction"));
				entity.setField(jo.getString("property"));
				sortList.add(entity);
			}
			gridParams.setSortList(sortList);
		}
	}
	public String getFilter() {
		return filter;
	}
	public void setFilter(String filter) {
		this.filter = filter;
		gridParams.setFilter(this.filter);
		
		if (this.filter != null && this.filter.length() > 0)
		{
			List<FilterEntity> filterList = gridParams.getFilterList();
			filterList.clear();
			JSONArray ja = JSONArray.fromObject(this.filter);
			for(int i = 0; i < ja.size(); i++)
			{
				JSONObject jo = ja.getJSONObject(i);
				
				String type = jo.getString("type");
				Object value = jo.get("value");
				
				FilterEntity entity = new FilterEntity();
				entity.setField(jo.getString("field"));
				entity.setType(type);
				entity.setValue(value);
				if (type.equals("boolean"))
				{
					entity.setValue(Boolean.valueOf(value.toString()) ? 1 : 0 );
				}
				if(type.equals("list"))
				{
					entity.setValue(Arrays.asList(JSONArray.fromObject(value).toArray()));
				}
				if (jo.containsKey("comparison"))
				{
					String comparison = jo.getString("comparison");
					if (comparison.equals("lt")){
						entity.setComparison(" < ");
					} 
					if (comparison.equals("gt")) {
						entity.setComparison(" > ");
					} 
					if (comparison.equals("eq")) {
						entity.setComparison(" = ");
					}
				}
				filterList.add(entity);
			}
		}
	}
	public GridParamsEntity getGridParams() {
		return gridParams;
	}
	public String getParentId() {
		return parentId;
	}
	public void setParentId(String parentId) {
		this.parentId = parentId;
		int index = this.parentId.indexOf("-1,");  //自动展开无法触发store的beforeload事件 ,
		if (index >= 0)
		{
			this.setJsonString(this.parentId.substring(3, this.parentId.length()));
			this.parentId = "-1";
		}
	}
	public File getUploadFile() {
		return uploadFile;
	}
	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}
	public String getUploadFileFileName() {
		return uploadFileFileName;
	}
	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}
	public String getUploadFileContentType() {
		return uploadFileContentType;
	}
	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}
	public InputStream getExcelStream() {
		return excelStream;
	}
	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}
	public String getExportFileName() {
		return exportFileName;
	}
	public void setExportFileName(String exportFileName) {
		this.exportFileName = exportFileName;
	}
}